
import json
import requests
from jinja2 import Environment, FileSystemLoader
from app.core.conf import ST
from app.models import Dev
from app.models.Station import Station
from cv.classify.classify_state import ClassifyState
from cv.classify.classify_th import ClassifyTH


class Tornado:

    def __init__(self):
        self._url= 'http://auks-security/tornado-cloud/SendTornadoMessage'
        self._header = {"Content-Type": "application/json;charset=UTF-8"}
        self.html = ''

    def PostRequest(self, data):
        datal = json.dumps(data) 
        res =requests.post(self._url, datal, self._header)
        return res.text

    def sendTo(self, desc):
        data = {
            "corp_id": "24338c6f0efd6c4bfb28f2ccbf9df6a4",
            "api_key": "a220205a-49cd-4a7c-930a-f30a8bb55bd7",
            "policyName": "AUKS_ML6KA0_00018",
            "userId": "K2207368",
            "desc":desc
        }
        return self.PostRequest(data)


    

    def send_dev_info(self,timestamp:int,station:Station,dev:Dev,short_cfy:ClassifyState,long_cfy:ClassifyTH=None):
        body = []
        if not long_cfy:
            result = {'dev_no': dev.dev_no, 'state': short_cfy.state_text}
        else:
            result = {'dev_no': dev.dev_no, 'state': short_cfy.state_text,'temp':long_cfy.temp_num, 'temp_set':long_cfy.temp_set_num, 'humi': long_cfy.humi_num, "humi_set": long_cfy.humi_set_num}
        body.append(result)
        self.generate_html(body, 2019, 2019)
        self.sendTo(self.html)



    def generate_html(self,body, starttime, stoptime):
        env = Environment(loader=FileSystemLoader(ST.TORNADO_TEMPLATE_PATH))
        template = env.get_template('template.html')
        html_content = template.render(start_time=starttime,stop_time=stoptime,body=body)
        self.html = html_content




tornadoDev:Tornado = Tornado()




if __name__=="__main__":
    dev_no = 10
    state = '运行'
    state_color = 'green'

    temp_set = 40.0
    temp = 40.1

    humi_set = 40.0
    humi = 30.0

    # desc = f"""
    # <table border="1" style="border-collapse: collapse;">
    #     <thead>
    #         <tr>
    #             <span style="margin-left:2rem;">{dev_no}号机台</span>
    #             <span style="margin-left:2rem; color:{state_color};">状态:{state}</span>
    #         </tr>
    #         <tr>
    #             <th>\</th>
    #             <th>设 定 值</th>
    #             <th>ֵ实 际 值</th>
    #             <th>ֵ趋 势</th>
    #             <th>状 态</th>
    #         </tr>
    #     </thead>
    #     <tbody style="text-align: center;">
    #         <tr>
    #             <td>温度</td>
    #             <td>{temp_set}℃</td>
    #             <td>ֵ{temp}℃</td>
    #             <td>ֵ{"↑" if temp > temp_set else "↓"}</td>
    #             <td style="color:{"green" if abs(temp-temp_set) < 3 else "red"};">ֵ{"OK" if abs(temp-temp_set) < 3 else "NG"}</td>
    #         </tr>
    #         <tr>
    #             <td>湿度</td>
    #             <td>{humi_set}%</td>
    #             <td>ֵ{humi}%</td>
    #             <td>ֵ{"↑" if humi > humi_set else "↓"}</td>
    #             <td style="color:{"green" if abs(humi-humi_set) < 3 else "red"};">ֵ{"OK" if abs(humi-humi_set) < 3 else "NG"}</td>
    #         </tr>
    #     </tbody>
    # </table>
    # """



    desc = f"""
    <table border="1" style="border-collapse: collapse;">
        <thead>
            <tr>
                <span style="margin-left:0rem;">{dev_no}号机台</span>
                <span style="margin-left:1rem;font-weight: bold;">状态:{state}</span>
            </tr>
            <tr>
                <th>\</th>
                <th>设 定 值</th>
                <th>ֵ实 际 值</th>
            </tr>
        </thead>
        <tbody style="text-align: center;">
            <tr>
                <td>温度</td>
                <td>{temp_set}℃</td>
                <td>ֵ{temp}℃</td>
            </tr>
            <tr>
                <td>湿度</td>
                <td>{humi_set}%</td>
                <td>ֵ{humi}%</td>
            </tr>
        </tbody>
    </table>
    """
    res = tornadoDev.sendTo(desc)
    print(res)
